/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.Type;

import fr.jade.fraclite.api.ComponentInterface;


/**
 * Provides a basic implementation of the {@link ComponentInterface} interface.
 */
public class BasicComponentInterface implements ComponentInterface {
  
  /**
   * The impl link
   */
  Object impl;
  
  /**
   * The component to which this component interface belongs.
   */
  protected Component owner;

  /**
   * The name of this component interface.
   */
  protected String name;

  /**
   * The type of this component interface.
   */
  protected Type type;

  /**
   * The flags of this component interfaces. These flags indicates if this
   * component interface is an internal interface or not, and if it has an
   * permanently associated interceptor or not.
   */
  protected boolean isInternal;
  
  /**
   * GUID of this interface
   * 
   */
  long guid;
  
  // -------------------------------------------------------------------------
  // Constructors
  // -------------------------------------------------------------------------

  /**
   * Constructs a new {@link BasicComponentInterface}.
   *
   * @param name the name of the interface.
   * @param type the type of the interface.
   * @param isInternal <tt>true</tt> if the interface is an internal interface.
   * @param impl the object that implements the interface.
   */

  public BasicComponentInterface (String name, Type type, boolean isInternal, Object impl) {
    this.name = name;
    this.type = type;
    this.isInternal = isInternal;
    this.setFcItfImpl(impl);
  }

  // -------------------------------------------------------------------------
  // Implementation of the Interface interface
  // -------------------------------------------------------------------------

  public Component getFcItfOwner () {
    return owner;
  }

  public String getFcItfName () {
    return name;
  }

  public Type getFcItfType () {
    return type;
  }

  public boolean isFcInternalItf () {
    return isInternal;
  }
  
  public long getFcItfGUId(){
    return guid;
  }
  
  // -------------------------------------------------------------------------
  // Implementation of the ComponentInterface interface
  // -------------------------------------------------------------------------

  public Object getFcItfImpl() {
    return impl;
  }

  public void setFcItfImpl(Object impl) {
    this.impl = impl;
  }
  
  public void setFcItfName (final String name) {
    this.name = name;
  }
  
  public void setFcItfOwner(Component owner){
    this.owner = owner;
  }
  
  public void setFcItfGUId(long guid){
    this.guid = guid;
  }

  // -------------------------------------------------------------------------
  // Overriden Object methods
  // -------------------------------------------------------------------------

  /**
   * Tests if the given object is equal to this component interface.
   *
   * @param o the object to be compared to this component interface.
   * @return <tt>true</tt> if o is a component interface with the same name and
   *      owner as this component interface, and if both are internal (resp.
   *      external) component interfaces.
   */
  @Override
  public boolean equals(final Object o){
    if (o instanceof Interface) {
      return guid==((Interface)o).getFcItfGUId();
    }else{
      return false;
    }
  }

  @Override
  public int hashCode(){
    return new Long(guid).intValue();
  }

}
